// $("#login").click(function () {
// });
var verify = sessionStorage.getItem('verify');

switch (verify) {
    case 'true':
        var name = sessionStorage.getItem('name');
        var account = sessionStorage.getItem('account');
        var character = sessionStorage.getItem('character');
        document.getElementById("name").innerText = name;
        document.getElementById("modalInfo_textAccount").innerText = account;

        break;

    case 'false':
        alert('请登录！');
        location.href = "login.html";
        break;
}

// //注销按钮事件
// $('#signOut').click(function () {
//     sessionStorage.setItem('verify', 'false');
//     var verify = sessionStorage.getItem('verify');
//     switch (verify) {
//         case 'false':
//             alert("请登录！");
//             location.href = "logon.html";
//     }
// });

//当修改密码modal隐藏时，清空三个输入框
$('#modalPsw').on('hidden.bs.modal', function () {
    document.getElementById('modalPsw_pswOld').value = '';
    document.getElementById('modalPsw_pswNew').value = '';
    document.getElementById('modalPsw_pswNew2').value = '';
});

//当修改信息modal隐藏时，清空输入框
$('#modalInfo').on('hidden.bs.modal', function () {
    document.getElementById('modalInfo_textName').value = '';
});

//修改密码事件
$('#modalPsw_submit').click(function () {
    var account = sessionStorage.getItem("account");
    var pswOld = $('#modalPsw_pswOld').val();
    var pswNew = $('#modalPsw_pswNew').val();
    var pswNew2 = $('#modalPsw_pswNew2').val();

    if (pswOld == '') {
        alert('原密码不能为空');
        return false;
    }

    if (pswNew == '') {
        alert('请输入新密码');
        return false;
    }

    if (pswNew == pswOld) {
        alert('新密码不能原密码与一致');
        return false;
    }

    if (pswNew2 == '') {
        alert('请重复新密码');
        return false;
    }

    if (pswNew != pswNew2) {
        alert('第二次输入不一致');
        return false;
    }

    $.ajax({
        url: '/user?method=changePsw',
        type: 'post',
        data: {account: account, pswOld: pswOld, pswNew: pswNew},
        dataType: 'json',
        success: function (result) {
            if (result) {
                alert('修改密码成功！');
                $('#modalPsw').modal('hide');
            }
            else {
                alert('修改密码失败！');
                document.getElementById('modalPsw_pswOld').value = '';
                document.getElementById('modalPsw_pswNew').value = '';
                document.getElementById('modalPsw_pswNew2').value = '';
            }
        }
    });
});

//修改信息事件
$('#modalInfo_btnSubmit').click(function () {
    var account = sessionStorage.getItem('account');
    var name = sessionStorage.getItem('name');
    var character = sessionStorage.getItem('character');

    var infoName = $('#modalInfo_textName').val();

    if (infoName == '') {
        alert("姓名不能为空");
        return false;
    }

    if (infoName == name) {
        alert('不能与旧名字一致');
        return false;
    }

    if (infoId == ''){
        alert('个人编号不能为空');
        return false;
    }

    $.ajax({
        url: '/user?method=changeInfo',
        type: 'post',
        data: {account: account, character: character, infoName: infoName},
        dataType: 'json',
        success: function (result) {
            if (result) {
                alert('修改成功！');
                sessionStorage.setItem('name', infoName);
                location.reload();
            }
            else {
                alert('修改失败！');
                document.getElementById('modalInfo_textName').value = '';
            }
        }
    });
});